package com.mde.xmlreader;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class XMLReader {
	
	private static final int MAX_LINES_COUNT = 100000;
	
	private static final int MAX_FILES_COUNT = 1000;

	private static final int FILES_COUNT = 241;
	
	private static final String STATION_TAG = "bus_stop";
	
	private static final double MIN_LAT = 52.038684;
	
	private static final double MAX_LAT = 52.155615;
	
	private static final double MIN_LON = 23.644099;
	
	private static final double MAX_LON = 23.831553;
	
	private static List<Double> lats = new ArrayList<Double>();
	
	private static List<Double> lons = new ArrayList<Double>();
	
	public static void main(String[] args) throws IOException {
	
		String filename = "BY.osm";
		File f = new File(filename);
		
		String fNameForStations = "BY.osm";
		
		Scanner scan = new Scanner(new File(fNameForStations));
		
		int nodesCount = 0;
		
		while (scan.hasNext()) {
			Node node = nextNode(scan);
			
			if (node != null && node.has(STATION_TAG)) {
				
					
				double lat = node.getLatitude(), lon = node.getLongitude();
				
				if (lat < MAX_LAT && lat > MIN_LAT && lon < MAX_LON && lon > MIN_LON) {
					++nodesCount;
					
					lats.add(node.getLatitude());
					lons.add(node.getLongitude());
					
					//System.out.println(node.toString());					
				}
				
			}
			
		}
		
		System.out.println(nodesCount);
		
		scan.close();			
		
		PrintWriter stationsOut = new PrintWriter(new File("stations.txt"));
		
		int len = lats.size();
		for (int i = 0; i < len; ++i) {
			stationsOut.write(lats.get(i) + " " + lons.get(i) + "\n");
		}
		
		stationsOut.close();
	}
	
	private static Node nextNode(Scanner scan) {
		String openingTag = null, closingTag = null;
		List<String> innerTags = new ArrayList<String>();
		
		if (scan.hasNext())
			openingTag = scan.nextLine();
		
		while (scan.hasNext() && !openingTag.contains("<node")) 
			openingTag = scan.nextLine();
		
		if (openingTag.contains("/>")) return null;
		if (scan.hasNext())
			closingTag = scan.nextLine();
		
		while (scan.hasNext() && !closingTag.contains("</node>")) {
			innerTags.add(closingTag);
			closingTag = scan.nextLine();
		}	
		
		return new Node(openingTag, closingTag, innerTags);
	}
	
	private static void findStation(String name, File f) throws FileNotFoundException {
		
		Scanner scan = new Scanner(f);
		int line = 0;
		
		while (scan.hasNext()) {
			String s = scan.nextLine();
			++line;
			
			if (s.contains(name)) {
				System.out.println(f.toString() + " " + s);
				
			}
		}
		
		scan.close();
	}

	private static void sliceToFIles(File f, String filenameNew) throws FileNotFoundException {
		Scanner scan = new Scanner(f);
		
		int filesCount = 0;
		int lineNumber = 0;
		
		PrintWriter file = new PrintWriter(new File(filenameNew + filesCount + ".txt"));
		
		while (scan.hasNext()) {
			String line = scan.nextLine();
			++lineNumber;
			
			if (lineNumber < MAX_LINES_COUNT) {
				file.write(line + "\n");
			} else {
				file.close();
				file = new PrintWriter(new File(filenameNew + ++filesCount + ".txt"));
				System.out.println(filesCount + " files created");
				file.write(line + "\n");
				lineNumber = 0;
			}
			
			if (filesCount > MAX_FILES_COUNT) break;
		}

		file.close();
		scan.close();
	}
	
}